package com.efrei.uchat.message;

import com.efrei.uchat.exception.UChatException;
import com.efrei.uchat.model.croom.CroomMessageStatus;

public class CroomMessage extends SystemMessage {
	
	private CroomMessageStatus status = CroomMessageStatus.UNKNOWN;
	private String croom;
	private String password;
	
	public CroomMessage(String text) throws UChatException {
		super(text);
		this.status = CroomMessageStatus.get(Integer.valueOf(fields.get(0)));
		this.croom = fields.get(1);
		if(status==CroomMessageStatus.JOIN && getNbFields()==5) {
			this.password = fields.get(2);
		}
	}
	
	/**
	 * create CroomMessage from scratch.
	 * @param status
	 * @param croom
	 * @param password null if not used
	 * @param sessionId
	 * @see CroomMessage#response(CroomMessage, CroomMessageStatus) helper function
	 */
	public CroomMessage(CroomMessageStatus status, String croom, String password, int sessionId) {
		super(Headers.CROOM_MESSAGE, sessionId);
		this.status = status;
		this.croom = croom;
		
		fields.clear();
		fields.add(status.toString());
		fields.add(croom);
		
		if(status==CroomMessageStatus.JOIN && password!=null) {
			this.password = password;
			fields.add(password);
		}
	}
	
	public CroomMessageStatus getStatus() {
		return status;
	}
	
	public String getCroom() {
		return croom;
	}
	
	public String getPassword() {
		return password;
	}
	
	/**
	 * generate CroomMessage response for current request
	 * @param response
	 * @return
	 */
	public CroomMessage response(CroomMessageStatus response) {
		return new CroomMessage(response, getCroom(), null, getSessionId());
	}
}
